﻿using System;
using System.Collections.Generic;
using NUnit.Framework;
using Xenta.Enums;
using Xenta.Operations;

namespace Xenta.Tests.Operations
{
    [TestFixture]
    public class StockEntryTests : OperationTest
    {
        protected override IEnumerable<Type> ModelMappings
        {
            get
            {
                return NHMappings.ALL;
            }
        }

        [TestCase(1, 1000, 100, 10)]
        public void Can_CRUD(decimal adjustmentMin,
            decimal adjustmentMax,
            decimal reserve,
            decimal exhaustionThreshold)
        {
            #region Prepare

            var country = Processor.Execute(new CreateCountry
            {
                Code = "xy"
            }).Result;
            var localizationProfile = Processor.Execute(new CreateLocalizationProfile
            {
                Code = "xy",
                LanguageID = Processor.Execute(new CreateLanguage
                {
                    Code = "xy"
                }).Result.EntityID,
                TimeZoneID = Processor.Execute(new CreateTimeZone
                {
                    Code = "xy"
                }).Result.EntityID,
                CurrencyID = Processor.Execute(new CreateCurrency
                {
                    Code = "xy",
                    Rate = 1
                }).Result.EntityID,
                CountryID = country.EntityID,
                RegionID = Processor.Execute(new CreateRegion
                {
                    Code = "xy",
                    CountryID = country.EntityID
                }).Result.EntityID,
                DimensionUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-dim",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Dimension
                }).Result.EntityID,
                WeightUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-weight",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Weight
                }).Result.EntityID,
                InformationUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-info",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Information
                }).Result.EntityID
            }).Result;
            var account = Processor.Execute(new CreateAccount
            {
                Email = "email@example.com",
                Username = "username",
                Password = "password",
                LocalizationProfileID = localizationProfile.EntityID
            }).Result;
            var seller = Processor.Execute(new CreateSeller
            {
                AccountID = account.EntityID
            }).Result;
            var catalogItem = Processor.Execute(new CreateCatalogItem
            {
                SupplierID = Processor.Execute(new CreateCatalogSupplier()).Result.EntityID
            }).Result;
            var salesItem = Processor.Execute(new CreateSalesItem
            {
                SellerID = seller.EntityID,
                CatalogItemID = catalogItem.EntityID,
                SKU = "xy",
                Price = 1
            }).Result;

            #endregion

            var entry = Processor.Execute(new CreateStockEntry
            {
                ItemID = salesItem.EntityID,
                AdjustmentMin = adjustmentMin,
                AdjustmentMax = adjustmentMax,
                Reserve = reserve,
                ExhaustionThreshold = exhaustionThreshold,
                Flags = StockEntryFlags.None
            }).Result;

            Assert.NotNull(entry);
            entry.AdjustmentMin.AreEqual(adjustmentMin);
            entry.AdjustmentMax.AreEqual(adjustmentMax);
            entry.Reserve.AreEqual(reserve);
            entry.ExhaustionThreshold.AreEqual(exhaustionThreshold);
            entry.AreEqual(Processor.Execute(new GetStockEntry
            {
                EntityID = entry.EntityID
            }).Result);
            entry.AreEqual(Processor.Execute(new GetStockEntryByItem
            {
                ItemID = salesItem.EntityID
            }).Result);
            entry.AreEqual(Processor.Execute(new GetStockEntryBySKU
            {
                SKU = salesItem.SKU
            }).Result);

            adjustmentMin = adjustmentMin * 2;
            adjustmentMax = adjustmentMax * 2;
            reserve = reserve * 2;
            exhaustionThreshold = exhaustionThreshold * 2;
            var upd_entry = Processor.Execute(new UpdateStockEntry
            {
                EntityID = entry.EntityID,
                AdjustmentMin = adjustmentMin,
                AdjustmentMax = adjustmentMax,
                Reserve = reserve,
                ExhaustionThreshold = exhaustionThreshold,
                Flags = StockEntryFlags.Active
            }).Result;

            Assert.NotNull(upd_entry);
            upd_entry.AreEqual(entry);
            upd_entry.AdjustmentMin.AreEqual(adjustmentMin);
            upd_entry.AdjustmentMax.AreEqual(adjustmentMax);
            upd_entry.Reserve.AreEqual(reserve);
            upd_entry.ExhaustionThreshold.AreEqual(exhaustionThreshold);
            upd_entry.Flags.AreEqual(StockEntryFlags.Active);

            Processor.Execute(new DeleteStockEntry
            {
                EntityID = entry.EntityID
            });
        }

        [TestCase(0, 1)]
        [TestCase(-1, 1)]
        [TestCase(1, 0)]
        [TestCase(1, -1)]
        public void Cannot_CreateUpdate_If_AdjustmentLtOrEqZero_Empty(decimal adjustmentMin, decimal adjustmentMax)
        {
            Assert.Throws<ApplicationException>(() => Processor.Execute(new CreateStockEntry
            {
                AdjustmentMin = adjustmentMin,
                AdjustmentMax = adjustmentMax
            }));
            Assert.Throws<ApplicationException>(() => Processor.Execute(new UpdateStockEntry
            {
                AdjustmentMin = adjustmentMin,
                AdjustmentMax = adjustmentMax
            }));
        }
    }
}
